#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "MSTK.h"


#ifdef __cplusplus
extern "C" {
#endif

  /* 
     Mark boundary vertices
     this function first mark all the vertices in the global
     mesh that are in submesh, then traverse these vertices,
     mark vertices with different number of neighbor faces 
     in global and submesh.
  */
int MESH_Surf_BuildPType(Mesh_ptr mesh, Mesh_ptr submesh) {
  int idx, vertex_globalid;
  MVertex_ptr global_mv,local_mv;
  MFace_ptr local_mf;
  List_ptr local_mvfaces, global_mvfaces;;

  /* Mark all the face to be interior */
  idx = 0;
  while((local_mf = MESH_Next_Face(submesh,&idx)))  
    MF_Set_PType(local_mf,PINTERIOR);
  
  /* Loop through vertices */
  idx = 0;
  while((local_mv = MESH_Next_Vertex(submesh,&idx))) {
    vertex_globalid = MV_GlobalID(local_mv);
    global_mv = MESH_VertexFromID(mesh,vertex_globalid);

    local_mvfaces = MV_Faces(local_mv);
    global_mvfaces = MV_Faces(global_mv);
    /* if the number of neighbor faces is different, it is a boundary vertex */
    if(List_Num_Entries(local_mvfaces) != List_Num_Entries(global_mvfaces))
      MV_Set_PType(local_mv,PBOUNDARY);
    else
      MV_Set_PType(local_mv,PINTERIOR);
    List_Delete(local_mvfaces);
    List_Delete(global_mvfaces);
  }

  return 1;
}
  
#ifdef __cplusplus
}
#endif

